package com.yanqu.road.server.logic.activity.stronghold.condition;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.event.EventTriggerArgs;
import com.yanqu.road.entity.event.IEventListener;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.strongholdwar.StrongholdActivityModule;
import com.yanqu.road.server.logic.activity.stronghold.StrongholdActivityData;
import com.yanqu.road.server.logic.activity.stronghold.condition.union.StrongholdActivityDamageUnionRankCondition;
import com.yanqu.road.server.logic.activity.stronghold.condition.union.StrongholdActivityScoreUnionRankCondition;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;

import java.math.BigInteger;

public abstract class BaseStrongholdActivityCondition {
    protected GamePlayer player;
    protected StrongholdActivityData activityData;
    protected ActivityConditionInfo conditionInfo;

    public BaseStrongholdActivityCondition(GamePlayer player, StrongholdActivityData activityData, ActivityConditionInfo conditionInfo) {
        this.player = player;
        this.activityData = activityData;
        this.conditionInfo = conditionInfo;
    }

    public abstract void addTrigger(GamePlayer player);

    public abstract void removeTrigger(GamePlayer player);

    public abstract boolean isCompleted(GamePlayer player);

    public static BaseStrongholdActivityCondition createActivityCondition(GamePlayer player, StrongholdActivityData activityData, ActivityInfo activityInfo, ActivityConditionInfo conditionInfo) {
        switch (conditionInfo.getType()) {
            case StrongholdActivityMgr.CONDITION_TYPE_WILD_USER:
                return new StrongholdActivityScoreRankCondition(player, activityData, conditionInfo);
            case StrongholdActivityMgr.CONDITION_TYPE_WILD_UNION:
                return new StrongholdActivityScoreUnionRankCondition(player, activityData, conditionInfo);
            case StrongholdActivityMgr.CONDITION_TYPE_ELIMINATE_USER:
                return new StrongholdActivityDamageRankCondition(player, activityData, conditionInfo);
            case StrongholdActivityMgr.CONDITION_TYPE_ELIMINATE_UNION:
                return new StrongholdActivityDamageUnionRankCondition(player, activityData, conditionInfo);
            default:
                return null;
        }
    }

    public final void setValue(BigInteger value) {
        UserActivityConditionData userActivityConditionData = getUserActivityConditionData(conditionInfo.getConditionId());
        if (null != userActivityConditionData) {
            userActivityConditionData.setValue(value);
        }
        player.getModule(StrongholdActivityModule.class).onActivityDataChange(activityData);
    }

    public BigInteger getPreValue() {
        UserActivityConditionData userActivityConditionData = getUserActivityConditionData(conditionInfo.getConditionId());
        if (null != userActivityConditionData) {
            return userActivityConditionData.getPreValue();
        } else {
            return BigInteger.ZERO;
        }
    }

    public final BigInteger getValue() {
        UserActivityConditionData userActivityConditionData = getUserActivityConditionData(conditionInfo.getConditionId());
        if (null != userActivityConditionData) {
            return userActivityConditionData.getValue();
        }
        return BigInteger.ZERO;
    }

    private UserActivityConditionData getUserActivityConditionData(int conditionId) {
        for (UserActivityConditionData userActivityConditionData : activityData.getUserActivityConditionDataList()) {
            if (userActivityConditionData.getConditionId() == conditionId) {
                return userActivityConditionData;
            }
        }
        return null;
    }

    protected abstract void player_BoardLevelHandle(Object object);

    protected final player_BoardLevelHandle handle = new player_BoardLevelHandle();

    public ActivityConditionInfo getConditionInfo() {
        return conditionInfo;
    }

    public void setPreValue(BigInteger value) {
        UserActivityConditionData userActivityConditionData = getUserActivityConditionData(conditionInfo.getConditionId());
        if (null != userActivityConditionData) {
            userActivityConditionData.setPreValue(value);
        }
    }

    public void initDefaultValue() {

    }

    public void setGetReward(boolean isGetReward) {
        UserActivityConditionData userActivityConditionData = getUserActivityConditionData(conditionInfo.getConditionId());
        if (null != userActivityConditionData) {
            userActivityConditionData.setGetReward(isGetReward);
        }
    }

    public void setModifyTime(long modifyTime) {
        UserActivityConditionData userActivityConditionData = getUserActivityConditionData(conditionInfo.getConditionId());
        if (null != userActivityConditionData) {
            userActivityConditionData.setModifyTime(modifyTime);
        }
    }

    public void initValue() {
        if (activityData.isInTime()) {
            if (getValue().compareTo(BigInteger.ZERO) == 0) {
                initDefaultValue();
            }
        }
    }

    public class player_BoardLevelHandle implements IEventListener {
        @Override
        public void triggerEvent(EventTriggerArgs eventArgs) {
            changeValue(eventArgs.getArgs());
        }
    }

    public void changeValue(Object object) {
        if (activityData.isShowTime()) {// 活动期间改变值
            player_BoardLevelHandle(object);
        }
    }

    public int getConditionId() {
        return conditionInfo.getConditionId();
    }

    public int getConditionType() {
        return conditionInfo.getType();
    }
}
